--****************************************************************************
--Table: BOMON
--****************************************************************************

--select
if exists(select name from sysobjects where name='sp_Select_BoMon')
drop proc sp_Select_BoMon
go
create proc sp_Select_BoMon
as 
begin
	select * from BOMON
end

--insert
if exists(select name from sysobjects where name='sp_Insert_BoMon')
drop proc sp_Insert_BoMon
go
create proc sp_Insert_BoMon
	@TenBoMon nvarchar(31)
as 
begin
	begin tran
	declare @MaBoMon int
	set @MaBoMon=1
	while exists(select * from BOMON where MaBoMon=@MaBoMon)
		set @MaBoMon=@MaBoMon+1

	if(exists(select * from BOMON where TenBoMon=@TenBoMon))
	begin
		raiserror (N'Thông tin này đã có trong CSDL!!!', 16,1)
		rollback tran
		return
	end
	else
	begin
		insert into BOMON values(@MaBoMon,@TenBoMon)
		commit tran	
	end
end

--delete
if exists(select name from sysobjects where name='sp_Delete_BoMon')
drop proc sp_Delete_BoMon
go
create proc sp_Delete_BoMon
	@MaBoMon int
as 
begin
	begin tran
	if not exists(select* from BOMON where MaBoMon=@MaBoMon)
	begin
		raiserror (N'Thông tin cần xoá không tồn tại!!!',16,1)
		rollback tran
		return
	end
	else 
		begin 
			Delete from BOMON where MaBoMon = @MaBoMon
			commit tran			
		end 
end

--Update
if exists(select name from sysobjects where name='sp_Update_BoMon')
drop proc sp_Update_BoMon
go
create proc sp_Update_BoMon
	@MaBoMon int,
	@TenBoMon nvarchar(31)
as 
begin
	begin tran
		Update BOMON set TenBoMon = @TenBoMon
		where MaBoMon = @MaBoMon
		commit tran	
end


